package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class test {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class cls = Class.forName("reflect.Person");
        Object obj = cls.newInstance();

        Method[] methods = cls.getDeclaredMethods();
        for(Method method : methods){
            //Method method1 = cls.getMethod(String.valueOf(method));
            //System.out.println(method1);
            //强行打开私有方法访问权限
            if(method.getParameterCount()==0
                    &&
               method.getModifiers()== Modifier.PUBLIC)//判断是否为公开方法
                {
                System.out.println("自动调用:"+method.getName()+"()");
                method.setAccessible(true);
                method.invoke(obj);
            }
        }
    }
}
